<template>
    <view class="bg-content">
        <u-upload ref="uUpload" :multiple="false" :auto-upload="false" :max-count="20" :show-progress="false" :file-list="sysFileList"
                  @on-remove="onRemove($event)" @on-choose-complete="onChooseComplete($event)"></u-upload>
    </view>
</template>

<script>

    import {QiniuCloudUtil} from "@/dev/core/utils/qiniu-cloud.util";
    import {SysFileService} from "@/dev/services/sys-file.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";

    export default {
        props: ["goodsId", "isGoodsEdit"],
        data() {
            return {
                fileList: [],
                sysFileList: []  // 已存在数据库的
            }
        },
        onReady() {
            const _self = this;
            if (this.isGoodsEdit) {
                this.findSysFileList();
            }
        },
        methods: {
            findSysFileList() {
                const _self = this;
                console.log("console.log(this.goodsId)", this.goodsId)
                new SysFileService().findSysFileList({
                    fkId: _self.goodsId,
                    type: "image/jpeg",
                    typeMark: "goodsDetail",
                }).then(result => {
                    result.forEach(item => {
                        _self.sysFileList.push({
                            id: item.id,
                            url: item.content + "?_timestamp=" + new Date().getTime(),
                            isImage: true
                        });
                        _self.fileList = [..._self.sysFileList];
                        _self.$forceUpdate();
                    });
                });
            },
            onRemove(index) {
                console.log(index);
                const _self = this;
                new SysFileService().deleteSysFile({
                    id: _self.fileList[index].id
                }).then(result => {
                    _self.fileList.splice(index, 1);
                    CommonUtil.toast("图片删除成功");
                    _self.$forceUpdate()
                });
            },
            onChooseComplete($event) {
                console.log($event);
                const _self = this;
                QiniuCloudUtil.uploadToCloud(_self.goodsId, $event[$event.length - 1].file.path, {
                    clearOldImage: false,
                    typeMark: "goodsDetail"
                }).then(result => {
                    if (result.status === "success") {
                        _self.fileList.push(result);
                        CommonUtil.toast(result.message);
                    } else {
                        CommonUtil.alert(result.message);
                    }
                });
            },
        }
    }
</script>

<style lang="scss" scoped>
</style>
